using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BUS;
using DAL;
using DTO;
using Utility;

public partial class Doanhnghiep_SuaSP_DN : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

        LoadDropDownList();
        LoadData();
    }

    private void LoadDropDownList()
    {
        tbDanhMucSanPhamCollection dmCollection = tbDanhMucSanPhamBUS.GetAll();
        ddl_dm.DataTextField = tbDanhMucSanPhamColumns.tendanhmuc.ToString();
        ddl_dm.DataValueField = tbDanhMucSanPhamColumns.madanhmuc.ToString();
        ddl_dm.DataSource = dmCollection;
        ddl_dm.DataBind();
    }

    private void LoadData()
    {
        try
        {
            int id = int.Parse(Request.QueryString["id"].ToString());
            txt_MaSP.Text = id.ToString();

            tbSanPham sp = tbSanPhamBUS.GetByID(id);
            if (sp.tinhtrang == 12)
            {
                txt_chietkhau.Enabled = true;
            }
            txt_TenSP.Text = sp.tensp;
            ddl_dm.SelectedValue = sp.danhmuc.ToString();
            txt_doanhnghiep.Text = sp.doanhnghiep.ToString();
            txt_tinhtrang .Text = sp.tinhtrang.ToString();
            txt_giagoc.Text = sp.giagoc.ToString();
            txt_giaban.Text = sp.giaban.ToString();
            txt_soluongban.Text = sp.soluongban.ToString();
            txt_soluongtoida.Text = sp.soluongtoida.ToString();
            txt_damua.Text = sp.damua.ToString();
            txt_chietkhau.Text = sp.chietkhau.ToString();
            txt_ngaysudung.Text = sp.ngaysudung.ToString();
            txt_ngayhh.Text = sp.ngayhethan.ToString();
            ftb_mota.Text = sp.mota;
            ftb_quydinh.Text = sp.quydinh;
            img_HinhMinhHoa.ImageUrl = "~/Photos/Thumb/" + sp.hinhchinh;
        }
        catch
        {
            Response.Redirect("~/Admin/SanPham_List.aspx");
        }
    }
    protected void btn_Update_Click(object sender, EventArgs e)
    {
            int id = int.Parse(Request.QueryString["id"].ToString());
            tbSanPham sp = tbSanPhamBUS.GetByID(id);
            sp.masp = int.Parse(txt_MaSP.Text.ToString());
            sp.tensp = txt_TenSP.Text;
            sp.danhmuc = int.Parse(ddl_dm.SelectedValue);
            sp.doanhnghiep = int.Parse(txt_doanhnghiep .Text);
            sp.tinhtrang = int.Parse(txt_tinhtrang .Text);
            sp.giaban = float.Parse(txt_giaban.Text);
            sp.giagoc = float.Parse(txt_giagoc.Text);
            sp.soluongban = float.Parse(txt_soluongban.Text);
            sp.soluongtoida = float.Parse(txt_soluongtoida.Text);
            sp.damua = int.Parse(txt_damua.Text);
            sp.chietkhau = float.Parse(txt_chietkhau.Text);
            sp.ngaysudung =DateTime.Parse(txt_ngaysudung.Text);
            sp.ngayhethan =DateTime .Parse (txt_ngayhh.Text);
            sp.quydinh = ftb_quydinh.Text;
            sp.mota = ftb_mota.Text;
            if (fu_HinhMinhHoa.HasFile)
            {
                string filename = DateTime.Now.Ticks.ToString() + "_" + fu_HinhMinhHoa.FileName;
                fu_HinhMinhHoa.SaveAs(Server.MapPath("~/Photos/" + filename));

                System.Drawing.Image img = System.Drawing.Image.FromFile(
                    Server.MapPath("~/Photos/" + filename));

                ImageUtility.MakeThumbnailPhoto(
                    img, 100, 100, Server.MapPath("~/Photos/Thumb/"), filename);
                // xoa hinh cu
                if (sp.hinhchinh != "no_images.jpg")
                {
                    System.IO.File.Delete(Server.MapPath("~/Photos/" + sp.hinhchinh));
                    System.IO.File.Delete(Server.MapPath("~/Photos/Thumb/" + sp.hinhchinh));
                }
                sp.hinhchinh = filename;
            }
            tbSanPhamBUS.Update(sp);
            Response.Redirect("~/DoanhNghiep/SanPham_DN.aspx?id="+sp.doanhnghiep .ToString ());
    }
}
